
import java.security.spec.RSAOtherPrimeInfo;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86131
 * Date: 2023-12-03
 * Time: 20:54
 */
public class MyLinkedList implements IList {

    static class ListNode {
        public int value;
        public ListNode next;
        public ListNode prev;

        public ListNode(int value) {  //构造方法
            this.value = value;
        }
    }

    public ListNode head;  //是个引用 默认null
    public ListNode last;  //是个引用 默认null

    @Override
    public void addFirst(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    @Override
    public void addLast(int data) {
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
        } else {
            node.prev = last;
            last.next = node;
            last = node;
        }
    }

    @Override
    public void addIndex(int index, int data) throws indexException {
        if (index < 0 || index > size()) {
            throw new indexException("index不合法: " + index);
        }
        ListNode node = new ListNode(data);
        if (head == null) {
            head = node;
            last = node;
            return;
        }
        if (index == 0) {
            addFirst(data);
            head = node;
        }
        if (index == size()) {
            addLast(data);
            last = node;
        }

        ListNode cur = head;
        for (int i = 0; i < index; i++) {
            cur = cur.next;
        }

        node.next = cur;
        cur.prev.next = node;
        node.prev = cur.prev;
        cur.prev = node;


    }


    //求当前链表是否存在Key
    @Override
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        //只有一个节点,且需要删除的
                        last = null;
                    }
                } else {
                    if (cur.next != null) {
                        //删除中间节点
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    } else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                return;
            } else {
                cur = cur.next;
            }
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.value == key) {
                if (cur == head) {
                    head = head.next;
                    if (head != null) {
                        head.prev = null;
                    } else {
                        //只有一个节点,且需要删除的
                        last = null;
                    }
                } else {
                    if (cur.next != null) {
                        //删除中间节点
                        cur.next.prev = cur.prev;
                        cur.prev.next = cur.next;
                    } else {
                        //删除尾巴节点
                        cur.prev.next = cur.next;
                        last = last.prev;
                    }
                }
                //这里不加return 就删除所有key的元素
            } else {
                cur = cur.next;
            }
        }
    }

    @Override
    public int size() {
        int count = 0;
        ListNode cur = head;  ////定义一个cur代替head去遍历
        while (cur != null) {  ////指向空就退出循环
            count++;
            cur = cur.next;
        }
        return count;
    }

    @Override
    public void clear() {  //清空
        this.head = null;
        this.last = null;
    }

    //打印列表
    @Override
    public void display() {
        ListNode cur = head;  ////定义一个cur代替head去遍历
        while (cur != null) {  ////指向空就退出循环
            System.out.println(cur.value + " ");
            cur = cur.next;
        }
        System.out.println();
    }
}
